<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/9
 * Time: 15:27
 */
namespace app\business\controller;

use think\Db;
use think\Request;
use think\Session;

class Mail extends Common
{
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 * 邮寄首页
	 * @param string get.title 要检索的字段
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('mail')->where('status','<>',0)->order('id desc');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('ordernumber|linkman|telephone','like',"{$title}");
			$where['query']['title'] = $title;
		}
		$result = $db->where('bid',Session::get('business.id'))->group('ordernumber')->paginate(8,false,$where);
		return view('index',compact('result'));
	}
	/**
	 * 订单详情
	 * @param string get.ordernumber 订单号
	*/
	public function detail()
	{
		$ordernumber = input('get.ordernumber');
		$result = Db::table('mail')->where('bid',Session::get('business.id'))
			->where('ordernumber',$ordernumber)->paginate(8);
		return view('detail',compact('result'));
	}
	
	/**
	 * 编辑
	 * @param string get.id 邮寄表id
	*/
	public function edit()
	{
		$id = input('get.id');
		$result = Db::table('mail')->where('id',$id)->where('bid',Session::get('business.id'))->find();
		return view('edit',compact('result'));
	}
	
	/**
	 * 更新
	 * @param string post.id 邮寄表id
	 * @param string post.linkman 邮寄人
	 * @param string post.telephone 联系电话
	 * @param string post.address 邮寄具体地址
	 * @param string post.province[0] 省
	 * @param string post.province[1] 市
	 * @param string post.province[2] 县
	 * @param string post.province[3] 镇(乡)
	 * @param string post.address 邮寄地址
	*/
	public function updatemail(Request $request)
	{
		if(request()->isAjax()){
			try{
				$info = $request->param();
				$input['linkman'] = $info['linkman'];
				$input['telephone'] = $info['telephone'];
				$input['address'] = $info['address'];
				//如果地址修改了
				if(count($info['province'])>1){
					$district = Db::table('district')->whereIn('id',$info['province'])->column('name');
					//省
					$input['province'] = $district[0];
					//市
					$input['city'] = $district[1];
					//县
					$input['county'] = $district[2];
					if(count($district) == 4){
						//镇
						$input['town'] = $district[3];
					}
				}
				$bool = Db::table('mail')->where('id',trim(input('post.id')))->update($input);
				if(!$bool){
					throw new \PDOException('修改失败');
				}
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 发货
	 * @param string post.id 邮寄表id
	 * @param string post.express 快递公司名称
	*/
	public function delivery()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$express = input('post.express');
				$result = Db::table('mail')->where('id',$id)->where('bid',Session::get('business.id'))->update(['status'=>2,'express'=>$express]);
				if(!$result){
					throw new \PDOException('发货失败');
				}
				$success = ['success'=>'发货成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 已到
	 * @param string post.id 邮寄表id
	*/
	public function reach()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('mail')->where('id',$id)->where('bid',Session::get('business.id'))->update(['status'=>3]);
				if(!$result){
					throw new \PDOException('操作失败');
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}